package com.lianzhihui.minitiktok.widget.view

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.RelativeLayout
import butterknife.ButterKnife
import com.bumptech.glide.Glide
import com.bytedance.tiktok.utils.NumUtils
import com.bytedance.tiktok.utils.OnVideoControllerListener
import com.echofeng.common.utils.AutoLinkHerfManager
import com.lianzhihui.minitiktok.bean.hot.VideoResponse
import com.lianzhihui.onlyleague.R
import kotlinx.android.synthetic.main.view_controller.view.*

/**
 * create by libo
 * create on 2020-05-20
 * description
 */
class ControllerView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context, attrs), View.OnClickListener {
    private var listener: OnVideoControllerListener? = null
    private var videoData: VideoResponse? = null

    private fun init() {
        val rootView = LayoutInflater.from(context).inflate(R.layout.view_controller, this)
        ButterKnife.bind(this, rootView)
        ivHead!!.setOnClickListener(this)
        ivComment!!.setOnClickListener(this)
        ivShare!!.setOnClickListener(this)
        rlLike!!.setOnClickListener(this)
        ivFocus!!.setOnClickListener(this)
        setRotateAnim()
    }

    fun setVideoData(videoData: VideoResponse) {
        this.videoData = videoData
        Glide.with(context).load(videoData.user!!.avatar).into(ivHead)
        tvNickname!!.text = "@" + videoData.user!!.nick
        AutoLinkHerfManager.setContent(videoData.title, autoLinkTextView)
        tvLikecount!!.text = NumUtils.numberFilter(videoData.like)
        tvCommentcount!!.text = NumUtils.numberFilter(videoData. comment)
        tvSharecount!!.text = NumUtils.numberFilter(0)
        animationView!!.setAnimation("like.json")

        //点赞状态
        if (videoData.is_like==1) {
            ivLike!!.setTextColor(resources.getColor(R.color.red))
        } else {
            ivLike!!.setTextColor(resources.getColor(R.color.white))
        }

        //关注状态
        if (videoData.user.is_follow==1) {
            ivFocus!!.visibility = GONE
        } else {
            ivFocus!!.visibility = VISIBLE
        }
        showAd(videoData);
    }

    fun showAd(data: VideoResponse){
        if (data.is_ad==1) {
            view_action.visibility = GONE
            Handler().postDelayed(Runnable {
                view_ad.visibility = VISIBLE
                view_conent.visibility = GONE
            }, 3000)
        }else{
            view_action.visibility = VISIBLE
            view_ad.visibility = GONE
            view_conent.visibility = VISIBLE
        }
    }
    fun setListener(listener: OnVideoControllerListener?) {
        this.listener = listener
    }

    override fun onClick(v: View) {
        if (listener == null) {
            return
        }
        when (v.id) {
            R.id.ivHead -> listener!!.onHeadClick()
            R.id.rlLike -> {
                listener!!.onLikeClick()
                like()
            }
            R.id.ivComment -> listener!!.onCommentClick()
            R.id.ivShare -> listener!!.onShareClick()
            R.id.ivFocus -> if (videoData!!.user.is_follow==0) {
                videoData!!.is_like = 1
                ivFocus!!.visibility = GONE
            }
        }
    }

    /**
     * 点赞动作
     */
    fun like() {
        if (videoData!!.is_like==0) {
            //点赞
            animationView!!.visibility = VISIBLE
            animationView!!.playAnimation()
            ivLike!!.setTextColor(resources.getColor(R.color.red))
            videoData!!.is_like = 1
        } else {
            //取消点赞
            animationView!!.visibility = INVISIBLE
            ivLike!!.setTextColor(resources.getColor(R.color.white))
            videoData!!.is_like = 0
        }

    }

    /**
     * 循环旋转动画
     */
    private fun setRotateAnim() {
        val rotateAnimation = RotateAnimation(0f, 359f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.repeatCount = Animation.INFINITE
        rotateAnimation.duration = 8000
        rotateAnimation.interpolator = LinearInterpolator()
        rlRecord!!.startAnimation(rotateAnimation)
    }

    init {
        init()
    }
}